package leetcode.hot100;

import java.util.Stack;

public class Solution20 {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char cur = s.charAt(i);
            if(cur=='('||cur=='{'||cur=='['){
                stack.push(cur);
                continue;
            }
            if(cur==')'){
                if(!stack.isEmpty()&&stack.peek()=='('){
                    stack.pop();
                    continue;
                }else return false;
            }
            if(cur=='}'){
                if(!stack.isEmpty()&&stack.peek()=='{'){
                    stack.pop();
                    continue;
                }else return false;
            }
            if(cur==']'){
                if(!stack.isEmpty()&&stack.peek()=='['){
                    stack.pop();
                    continue;
                }else return false;
            }
        }
        return stack.isEmpty();
    }
}
